@page
@using System.Web
@using Microsoft.AspNetCore.Mvc.Localization
@using Microsoft.Extensions.Localization
@using Volo.Abp.AspNetCore.Mvc.UI.Bootstrap.TagHelpers.Modal
@using Volo.Abp.FeatureManagement.Localization
@using Volo.Abp.Validation.StringValues
@using Volo.Abp.FeatureManagement.Web.Pages.FeatureManagement
@model FeatureManagementModal
@inject IHtmlLocalizer<AbpFeatureManagementResource> L
@inject IStringLocalizerFactory StringLocalizerFactory
@{
    Layout = null;

    IHtmlLocalizer CreateHtmlLocalizer(string resourceName)
    {
        var localizer = StringLocalizerFactory.CreateByResourceNameOrNull(resourceName) ??
                        StringLocalizerFactory.CreateDefaultOrNull();
        return new HtmlLocalizer(localizer);
    }
}

<abp-script src="/Pages/FeatureManagement/feature-management-modal.js" />
<abp-style src="/Pages/FeatureManagement/feature-management-modal.css" />

<form method="post" asp-page="/FeatureManagement/FeatureManagementModal" data-script-class="abp.modals.FeatureManagement" id="FeatureManagementForm">
    <abp-modal id="featureManagmentModal" size="Large">
        <abp-modal-header title="@(L["Features"].Value + Model.ProviderKeyDisplayName)"></abp-modal-header>
        @if (Model.FeatureListResultDto != null && Model.FeatureListResultDto.Groups.Any())
        {
            var featureGroups = Model.FeatureListResultDto.Groups;

            <abp-modal-body>
                <input asp-for="@Model.ProviderKey"/>
                <input asp-for="@Model.ProviderName"/>
                <abp-tabs name="FeaturesTabs" tab-style="PillVertical" vertical-header-size="_4" class="custom-scroll-container">
                    @for (var i = 0; i < featureGroups.Count; i++)
                    {
                        var featureGroup = featureGroups[i];
                        <abp-tab title="@featureGroup.DisplayName" name="v-pills-tab-@featureGroup.GetNormalizedGroupName()">
                            <h4>@featureGroup.DisplayName</h4>
                            <hr class="mt-2 mb-3"/>
                            <div class="custom-scroll-content" style="height: 400px;">
                                <div class="pl-1 pt-1">
                                    @for (var j = 0; j < featureGroup.Features.Count; j++)
                                    {
                                        var feature = featureGroup.Features[j];
                                        var disabled = Model.IsDisabled(feature);
                                        <div class="mt-2">

                                            <input type="text" abp-id-name="@Model.FeatureGroups[i].Features[j].IsDisabled" value="@(disabled.ToString())" hidden/>
                                            <input type="text" abp-id-name="@Model.FeatureGroups[i].Features[j].Type" value="@feature.ValueType?.Name" hidden/>
                                            @if (feature.ValueType is ToggleStringValueType)
                                            {
                                                <abp-input asp-for="@feature.Value"
                                                    type="checkbox"
                                                    class="d-inline"
                                                    abp-id-name="@Model.FeatureGroups[i].Features[j].BoolValue"
                                                    label="@Model.GetShownName(feature)"
                                                    disabled="disabled"
                                                    group-data-feature-name="@feature.Name"
                                                    group-data-parent-name="@(feature.ParentName ?? "")"
                                                    group-style="margin-inline-start: @(feature.Depth * 20)px"/>
                                                if (feature.Description != null)
                                                {
                                                    <div class="form-text" style="margin-inline-start: @(feature.Depth * 20)px">@feature.Description</div>
                                                }
                                            }

                                            @if (feature.ValueType is FreeTextStringValueType)
                                            {
                                                var type = feature.ValueType.Validator is NumericValueValidator ? "number" : "text";
                                                <abp-input asp-for="@feature.Value"
                                                    label="@Model.GetShownName(feature)"
                                                    abp-id-name="@Model.FeatureGroups[i].Features[j].Value"
                                                    type="@type"
                                                    disabled="disabled"
                                                    group-data-feature-name="@feature.Name"
                                                    group-data-parent-name="@(feature.ParentName ?? "")"
                                                    group-style="margin-inline-start: @(feature.Depth * 25)px"/>
                                                if (feature.Description != null)
                                                {
                                                    <div class="form-text" style="margin-inline-start: @(feature.Depth * 25)px">@feature.Description</div>
                                                }
                                            }

                                            @if (feature.ValueType is SelectionStringValueType selectType)
                                            {
                                                <div data-feature-name="@feature.Name" data-parent-name="@(feature.ParentName ?? "")" style="margin-inline-start: @(feature.Depth * 25)px" class="mb-3">
                                                    <label class="form-label" for="@feature.Name">@Model.GetShownName(feature)</label>
                                                    <select id="@feature.Name" name="FeatureGroups[@i].Features[@j].Value" class="form-select" @(disabled ? "disabled" : "")>
                                                        @foreach (var item in selectType.ItemSource.Items)
                                                        {
                                                            var selected = item.Value == feature.Value ? "selected=\"selected\"" : "";
                                                            <!option value="@item.Value" @(selected) >@CreateHtmlLocalizer(item.DisplayText.ResourceName).GetString(item.DisplayText.Name) </!option>
                                                        }
                                                    </select>
                                                    @if (feature.Description != null)
                                                    {
                                                        <div class="form-text" style="margin-inline-start: @(feature.Depth * 25)px">@feature.Description</div>
                                                    }
                                                </div>
                                            }
                                            <input value="@feature.Name" abp-id-name="@Model.FeatureGroups[i].Features[j].Name" hidden/>

                                        </div>
                                    }
                                </div>
                            </div>
                        </abp-tab>
                    }

                </abp-tabs>
            </abp-modal-body>
            <abp-modal-footer>
                <abp-button data-bs-dismiss="modal" button-type="Link">@L["Cancel"]</abp-button>
                <abp-button button-type="Outline_Primary" id="ResetToDefaults"><i class="fa fa-refresh"></i> @L["ResetToDefault"]</abp-button>
                <abp-button button-type="Primary" type="submit"><i class="fa fa-check"></i> @L["Save"]</abp-button>
            </abp-modal-footer>
        }
        else
        {
            <abp-modal-body class="my-2">
                @L["NoFeatureFoundMessage"]
            </abp-modal-body>
        }
    </abp-modal>
</form>
